﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;
using VCN.App.DataTransferObjects;

namespace VCN.admin.UC
{
    public partial class AddPartnerUc : System.Web.UI.UserControl
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                drdtype.Enabled = true;
                logoNotNull.Visible = false;
                if (Request.QueryString["new"] != null)
                {
                    drdtype.SelectedValue = Request.QueryString["new"].ToString();
                    drdtype.Enabled = false;
                }
                if (Request.QueryString["PId"] != null)
                {
                    ImageLogoDAO m_logo_dao = new ImageLogoDAO();
                    ImageLogo m_obj = m_logo_dao.selectImageLogoByID(int.Parse(Request.QueryString["PId"].ToString()));

                    if (m_obj == null)
                    {
                        return;
                    }
                    txtLinkWebsite.Text = m_obj.Link;
                    drdstatus.SelectedValue = ((m_obj.Status == true) ? 0 : 1).ToString();
                    drdtype.SelectedValue = m_obj.Type.ToString(); // 0: logo công ty; 2: Ảnh đối tác
                    drdtype.Enabled = false;
                    txtName.Text = m_obj.Name;                                      
                    chkUseStatus.Visible = true;                    
                    txtViTri.Text = m_obj.Order.ToString();                    
                }
                else
                {
                }
            }
        }

        protected void m_cmd_thoat_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/admin/default.aspx");
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void m_cmd_luu_du_lieu_Click1(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["PId"] != null) // Update
                {
                    ImageLogo v_obj = new ImageLogoDAO().selectImageLogoByID(int.Parse(Request.QueryString["PId"].ToString()));

                    if (chkUseStatus.Checked == false)
                    {
                        if (UploadObj.HasFile)
                        {
                            UploadObj.EnableRename = false;
                            UploadObj.ContentFileType = "jpg,jpeg,swf,gif,png";
                            UploadObj.SaveToDirectory = "../home/Images/Logo/";
                            v_obj.Image = UploadObj.StartUpload();
                        }                        
                    }
                    v_obj.Name = txtName.Text;
                    v_obj.Status = (drdstatus.SelectedIndex == 0) ? true : false;
                    v_obj.Link = txtLinkWebsite.Text;
                    v_obj.Type = (drdtype.SelectedIndex == 0) ? 0 : 2;
                    v_obj.Order = int.Parse(txtViTri.Text);

                    if (new ImageLogoDAO().update(v_obj))
                    {
                        lblMsg.Text = "Update thành công";
                        lblMsg.Visible = true;
                        Response.Redirect("/admin/viewpartner.aspx");
                    }
                    else
                    {
                        lblMsg.Text = "Update thất bại";
                        lblMsg.Visible = true;
                    }
                }
                else
                {
                    if (!UploadObj.HasFile)
                    {
                        logoNotNull.Text = "Logo không được bỏ trống.";
                        logoNotNull.Visible = true;
                    }
                    else if (txtName.Text.Trim() == "")
                    {
                        lblMsg.Text = "Tên mô tả không được bỏ trống";
                        lblMsg.Visible = true;
                    }
                    else
                    {
                        UploadObj.EnableRename = false;
                        UploadObj.ContentFileType = "jpg,jpeg,swf,gif,png";
                        UploadObj.SaveToDirectory = "../home/Images/Logo/";
                        new ImageLogoDAO().insert(new ImageLogo(txtName.Text, txtLinkWebsite.Text, UploadObj.StartUpload(), (drdtype.SelectedIndex == 0) ? 0 : 2, (drdstatus.SelectedIndex == 0) ? true : false, int.Parse(txtViTri.Text)));                        
                        Response.Redirect("/admin/viewpartner.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
                lblMsg.Visible = true;
            }
        }
    }
}